from typing import Optional

import PySide6.QtGui
import PySide6.QtWidgets
from PySide6.QtCore import QMimeData, Qt
from PySide6.QtGui import QDrag, QPixmap
from PySide6.QtWidgets import QListWidget


class DragableListWidget(QListWidget):
    '''
    description:
        一个支持拖拽的 QListWidget，拖拽的对象内部数据为ListWidgetItem
        所显示的字符串
    args:
        parent 'QWidget' 父窗体
    return:
        None
    '''
    def __init__(
        self, 
        parent: Optional[PySide6.QtWidgets.QWidget] = None) -> None:
        #################################
        super().__init__(parent=parent)
        self.setMouseTracking(True)
        
    
    def mousePressEvent(self, event: PySide6.QtGui.QMouseEvent) -> None:
        # return super().mousePressEvent(event)
        if (event.button() == Qt.LeftButton):
            if (self.dragEnabled()):
                drag_ = QDrag(self)
                item_ = self.itemAt(event.pos())
                mime_data_ = QMimeData()
                mime_data_.setText(item_.text())
                drag_.setMimeData(mime_data_)
                drag_.exec()

    def mouseMoveEvent(self, e: PySide6.QtGui.QMouseEvent) -> None:
        # return super().mouseMoveEvent(e)
        if (self.itemAt(e.pos())):
            item_ = self.itemAt(e.pos())
            self.setDragEnabled(True)
        else:
            self.setDragEnabled(False)
